<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程购买订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('subject.Order');
        // $this->view->assign("payList", $this->model->getPayList());
    }

    public function del($ids = null)
    {

        $ids = $ids ?: $this->request->param("ids", '', 'trim');

        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }

        $list = $this->model->where(['id' => ['IN', $ids]])->select();

        if (empty($list)) {
            $this->error(__('No rows were deleted'));
        }

        //删除
        $result = $this->model->destroy($ids);

        if ($result === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    //回收站
    public function recyclebin()
    {
        if ($this->request->isAjax()) {
            //设置过滤方法
            $this->request->filter(['strip_tags', 'trim']);

            //如果发送的来源是 Selectpage，则转发到 Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            //通过fastadmin封装函数处理前端发过来的参数
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();

            $list = $this->model
                ->onlyTrashed()
                ->where($where)          //搜索条件
                ->order($sort, $order)         //$sort排序字段,$order降序还是升序
                ->paginate($limit);    //调用分页函数

            $result = ['total' => $list->total(), 'rows' => $list->items()];      //返回排序总数total()和排序数据items()
            return json($result);    //返回数据
        }

        return $this->fetch();
    }

    //还原
    public function restore($ids = null)
    {
        if ($this->request->isAjax()) {
            $ids = $ids ?: $this->request->param('ids', '', 'trim');

            if (empty($ids)) {
                $this->error(__('Parameter %s can not be empty', 'ids'));
            }

            $list = $this->model->onlyTrashed()->select($ids);

            if (!$list) {
                $this->error(__('No rows were deleted'));
            }

            $result = $this->model->onlyTrashed()->where('id', 'IN', $ids)->update(['deletetime' => null]);

            if ($result === false) {
                $this->error('还原失败');
            } else {
                $this->success();
            }
        }
    }
}
